//
//  ViewController.m
//  瀑布流
//
//  Created by mac on 2017/3/15.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "WaterFallView.h"
#import "WaterFallViewCell.h"


@interface ViewController ()<WaterFallViewDelegate, WaterFallViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //1.
    [self createWaterFall];

}

- (void)createWaterFall{
    WaterFallView *waterFall = [[WaterFallView alloc] init];
    waterFall.frame = self.view.bounds;
    
    waterFall.delegate = self;
    waterFall.dataSource = self;
    [self.view addSubview:waterFall];


}

#pragma mark ----- 代理方法
//数据源
- (NSUInteger)numberOfCellsInWaterfall:(WaterFallView *)waterFallView{

    return 100;
}
//多少列
- (NSUInteger)numberOfColumnsInWaterfall:(WaterFallView *)waterFallView{

    return 3;
}
//cell
- (WaterFallViewCell *)waterFallView:(WaterFallView *)waterFallView cellAtIndex:(NSUInteger)index{
    static NSString *ID = @"cell";
    WaterFallViewCell *cell = [waterFallView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[WaterFallViewCell alloc] init];
        cell.identifier = ID;
        cell.backgroundColor = HMRandomColor;
        
        UILabel *label = [[UILabel alloc] init];
        label.tag = 10;
        label.frame = CGRectMake(0, 0, 50, 20);
        [cell addSubview:label];
    }
    
    UILabel *lab = (UILabel *)[cell viewWithTag:10];
    lab.text = [NSString stringWithFormat:@"%ld", index];
    
    //如何验证循环利用：打印地址
    NSLog(@"%ld==== %p", index, cell);
    return cell;
}
//cell 高
- (CGFloat)waterFallView:(WaterFallView *)waterFallView heightAtIndex:(NSUInteger)index{
    //当前列号：0%3=0 -》商：0，余：0；1%3=1-》商0，余1；2%3=2，商0，余2
    NSUInteger result = index % 3;
    
    if (result == 0) {
        //第0列
        return 70;
    }else if (result == 1){
        //第1列
        return 100;

    }else if (result == 2){
        //第2列
        return 80;
    }
    return 80;
}

- (CGFloat)waterFallView:(WaterFallView *)waterFallView marginType:(WaterFallMarginType)marginType{

    switch (marginType) {
        case WaterFallMarginTypeTop:
        case WaterFallMarginTypeLeft:
        case WaterFallMarginTypeRight:
        case WaterFallMarginTypeBottom:
            
            return 10;
            
        default: return 5;
    }
}

- (void)waterFallView:(WaterFallView *)waterFallView didSelectAtIndex:(NSUInteger)index{

    NSLog(@"点击了cell--- %ld", index);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
